import paramiko

username = 'root'
password = 'root'
port = 22
hostname = '172.20.10.12'


if __name__ == '__main__':

    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname=hostname, port=port, username=username, password=password)

    command = 'pwd'

    stdin, stdout, stderr = ssh.exec_command(command)
    content = stdout.read().decode('utf-8') + stderr.read().decode('utf-8')
    print(content)

    localpath = 'test.ssh.txt'
    remotepath = '/root/test.ssh.txt'

    transport = None
    try:
        transport = paramiko.Transport((hostname, port))
        transport.connect(username=username, password=password)
        sftp = paramiko.SFTPClient.from_transport(transport)

        # # 执行上传动作
        # sftp.put(localpath, remotepath)

        # 执行下载动作
        sftp.get(remotepath, localpath)
    except Exception as e:
        print(e)
    finally:
        if transport is not None:
            transport.close()

